<?php
/**
 * Description of c_template
 *
 * @author tonatos
 */
class c_template {

    /*
     * @the registry
     * @access private
     */
    private $module;

    /*
     * @Variables array
     * @access private
     */
    private $vars = array();

    /**
     *
     * @constructor
     *
     * @access public
     *
     * @return void
     *
     */
    function __construct($module) {
        $this->module = $module;
        $this->module_link = "index.php?a=112&id=".(int)$_REQUEST['id'];

        //Подключаем языковой пакет
        $filename = MVC_BASE_PATH."lang/$this->module/russian-UTF8.inc.php";

        if (file_exists($filename)){
            include($filename);
        }
    }


     /**
     *
     * @set undefined vars
     *
     * @param string $index
     *
     * @param mixed $value
     *
     * @return void
     *
     */
     public function __set($index, $value)
     {
            $this->vars[$index] = $value;
     }

     public function copy_vars($data) {
         foreach ($data as $key=>$value) {
             $this->$key=$value;
         }
     }

    function show($name, $return=false) {
        $path = MVC_BASE_PATH."views/$this->module/$name.php";
        if (file_exists($path) == false)
        {
                throw new Exception('Template not found in '. $path);
                return false;
        }

        // Load variables
        foreach ($this->vars as $key => $value)
        {
                $$key = $value;
        }

        if ($return) ob_start();

        include ($path);

        if ($return){
            $content = ob_get_contents();
            ob_end_clean();
            return $content;
        }
    }

    public function price_format($output){
        $phx_format = MODX_BASE_PATH."assets/plugins/phx/modifiers/price.phx.php";

        $file_contents = @file_get_contents($phx_format);
        $file_contents = str_replace('<?php', '', $file_contents);
        $file_contents = str_replace('?>', '', $file_contents);
        $file_contents = str_replace('<?', '', $file_contents);

        return eval($file_contents);
    }

    public function address_format($address){
        $address_controller = new c_address_controller($address);

        return $address_controller->format();
    }

}

?>
